﻿@{
    Layout = "~/Views/Shared/_Index.cshtml";
}
<div class="container-div">
    <div class="row">
        <div id="searchDiv" class="col-sm-12 search-collapse">
            <div class="select-list">
                <ul>
                    <li>
                        字典类：<input id="dictType" col="DictType" type="text" readonly="readonly" />
                    </li>
                    <li>
                        字典键：<input id="dictKey" col="DictKey" type="text" />
                    </li>
                    <li>
                        字典值：<input id="dictValue" col="DictValue" type="text" />
                    </li>
                    <li>
                        <a id="btnSearch" class="btn btn-primary btn-sm" onclick="searchGrid()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                    </li>
                </ul>
            </div>
        </div>
        <div class="btn-group-sm" id="toolbarPermission">
            <a id="btnAdd" class="btn btn-success" onclick="showSaveForm(true)"><i class="fa fa-plus"></i> 新增</a>
            <a id="btnEdit" class="btn btn-primary disabled" onclick="showSaveForm(false)"><i class="fa fa-edit"></i> 修改</a>
            <a id="btnDelete" class="btn btn-danger disabled" onclick="deleteForm()"><i class="fa fa-remove"></i> 删除</a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="gridTable" data-mobile-responsive="true"></table>
        </div>
    </div>
</div>

<script type="text/javascript">

    var dictType = ys.request("dictType");

    $(function () {
        $("#dictType").val(dictType);

        initGrid();
    });

    function initGrid() {
        var queryUrl = '@Url.Content("~/SystemManage/DataDictDetail/GetPageListJson")';
        $('#gridTable').ysTable({
            url: queryUrl,
            sortName: 'DictSort',
            sortOrder: 'Asc',
            toolbar: '#toolbarPermission',
            columns: [
                { checkbox: true, visible: true },
                { field: 'Id', title: 'Id', visible: false },
                { field: 'DictKey', title: '字典键' },
                { field: 'DictValue', title: '字典值' },
                {
                    field: 'IsDefault', title: '系统默认', formatter: function (value, row, index) {
                        return ys.getValueByKey(ys.getJson(@Html.Raw(typeof(IsEnum).EnumToDictionaryString())), value);
                    }
                },
                {
                    field: 'ListClass', title: '显示样式', formatter: function (value, row, index) {
                        if (value) {
                            return '<span class="badge badge-' + value + '">&nbsp;&nbsp;&nbsp;&nbsp;</span>';
                        }
                    }
                },
                {
                    field: 'DictStatus', title: '状态', formatter: function (value, row, index) {
                        if (row.DictStatus == "@StatusEnum.Yes.ParseToInt()") {
                            return '<span class="badge badge-primary">' + "@StatusEnum.Yes.GetDescription()" + '</span>';
                        } else {
                            return '<span class="badge badge-warning">' + "@StatusEnum.No.GetDescription()" + '</span>';
                        }
                    }
                },
                {
                    field: 'BaseModifyTime', title: '创建时间', formatter: function (value, row, index) {
                        return ys.formatDate(value, "yyyy-MM-dd HH:mm:ss");
                    }
                }
            ],
            queryParams: function (params) {
                var pagination = $('#gridTable').ysTable('getPagination', params);
                var queryString = $('#searchDiv').getWebControls(pagination);
                return queryString;
            }
        })
    }

    function searchGrid() {
        $('#gridTable').ysTable('search');
        resetToolbarStatus();
    }

    function showSaveForm(bAdd) {
        var id = 0;
        if (!bAdd) {
            var selectedRow = $("#gridTable").bootstrapTable("getSelections");
            if (!ys.checkRowEdit(selectedRow)) {
                return;
            }
            else {
                id = selectedRow[0].Id;
            }
        }
        ys.openDialog({
            title: id > 0 ? "编辑字典数据" : "添加字典数据",
            content: '@Url.Content("~/SystemManage/DataDictDetail/DataDictDetailForm")' + '?id=' + id + '&dictType=' + dictType,
            callback: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']];
                iframeWin.saveForm(index);
            }
        });
    }

    function deleteForm() {
        var selectedRow = $("#gridTable").bootstrapTable("getSelections");
        if (ys.checkRowDelete(selectedRow)) {
            ys.confirm("确认要删除选中的" + selectedRow.length + "条数据吗？", function () {
                var ids = ys.getIds(selectedRow);
                ys.ajax({
                    url: '@Url.Content("~/SystemManage/DataDictDetail/DeleteFormJson")' + '?ids=' + ids,
                    type: "post",
                    success: function (obj) {
                        if (obj.Tag == 1) {
                            ys.msgSuccess(obj.Message);
                            searchGrid();
                        }
                        else {
                            ys.msgError(obj.Message);
                        }
                    }
                });
            });
        }
    }
</script>
